package org.phalanx.client.dialogs;

import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.phalanx.client.PhalanxClient;
import org.phalanx.client.UI.PhalanxPanelManager;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.dialogs.servernode.ServerNodeConnectionPanel;
import org.phalanx.client.mapeditor.PhalanxMapEditorPanel;
import org.phalanx.client.net.XMPPConnectionThread;

public class PhalanxWindow extends JFrame implements pButtonListener{
	private JPanel currentContextPanel;
	private JPanel mainMenuContainerPanel;
	private JPanel masterPanel;
	private JTabbedPane tabbedPanel;
	private MainMenuPanel pMenuPanel;
	private PhalanxOnlineStatusPanel pStatusPanel;
	public static final String NAME = "PhalanxWindow";
	public static final String MASTER_PANEL_NAME = "MASTERPANEL";
	
	public PhalanxWindow(){
		initDefaultLook();
	}
	
	private void initDefaultLook(){
		createFrameLookAndFeel();
		createBodyPanel();
		setCurrentContextPanel(mainMenuContainerPanel);
		tabbedPanel = createTabbedPane();
		masterPanel.add(tabbedPanel);
		this.add(masterPanel);
		
		PhalanxPanelManager.getInstance().setPanel(MainMenuPanel.NAME, pMenuPanel);
		PhalanxPanelManager.getInstance().setPanel(PhalanxOnlineStatusPanel.NAME, pStatusPanel);
		PhalanxPanelManager.getInstance().setPanel(MASTER_PANEL_NAME, mainMenuContainerPanel);
	}
	
	private void createFrameLookAndFeel(){
		masterPanel = createJPanel();
		masterPanel.setLayout(new BoxLayout(masterPanel, BoxLayout.X_AXIS));
		this.setTitle("Phalanx version " + PhalanxClient.getClientInstance().formattedVersion());
		this.setBackground(Color.black);
		this.setSize(900, 700);
		this.setResizable(true);
	}
	
	private JTabbedPane createTabbedPane(){
		JTabbedPane tempPane = new JTabbedPane();
		tempPane.setBackground(Color.black);
		tempPane.addTab("Main Menu Screen", getCurrentContextPanel());
		return tempPane;
	}
	
	private void createBodyPanel(){
		mainMenuContainerPanel = createJPanel();
		mainMenuContainerPanel.setLayout(new BoxLayout(mainMenuContainerPanel,BoxLayout.Y_AXIS));
		
		pMenuPanel = new MainMenuPanel(this);
		pStatusPanel = pMenuPanel.getStatusPanel();
		
		mainMenuContainerPanel.add(pMenuPanel);
		
		PhalanxPanelManager.getInstance().setPanel("MAINMENUSTATUS", mainMenuContainerPanel);
		
	}
	
	public JTabbedPane getTabbedPanel(){
		return tabbedPanel;
	}

	public JPanel getCurrentContextPanel() {
		return currentContextPanel;
	}

	public void setCurrentContextPanel(JPanel currentContextPanel) {
		this.currentContextPanel = currentContextPanel;
	}
	
	private JPanel createJPanel(){
		JPanel tempPanel = new JPanel();
		tempPanel.setBackground(Color.black);
		return tempPanel;
	}

	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent().equals(pMenuPanel.getExitButton())){
			System.exit(0);
		}
		else if (p.getComponent().equals(pMenuPanel.getConnectToServer())){
			// Start up Connection Thread
			if (!PhalanxClient.getClientInstance().getXMPPConnectionManager().getIsConnected()){
				new XMPPConnectionThread();
			} else{
				PhalanxClient.getClientInstance().getXMPPConnectionManager().disconnect();
			}
		}
		else if (p.getComponent().equals(pMenuPanel.getServerNodeButton())){
			replacePanel(new ServerNodeConnectionPanel(this));
		}
		else if (p.getComponent().equals(pMenuPanel.getMapEditorButton())){
			PhalanxMapEditorPanel editor = (PhalanxMapEditorPanel)PhalanxPanelManager.getInstance().getPanel(PhalanxMapEditorPanel.NAME);
			if (editor == null){
				editor = new PhalanxMapEditorPanel(this);
				PhalanxPanelManager.getInstance().setPanel(PhalanxMapEditorPanel.NAME,editor);
				editor.initializePanel();
			}
			replacePanel(editor);
		}
	}
	
	public void replacePanel(JPanel newPanel){
		((JPanel)this.tabbedPanel.getComponent(0)).removeAll();
		((JPanel)this.tabbedPanel.getComponent(0)).add(newPanel);
		this.repaint();
	}
}
